﻿using MyModels;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MyRepository.BASE
{
    public interface IBaseRepository<TEntity> where TEntity : class
    {
        /// <summary>
        /// SqlSugarClient 实体
        /// </summary>
        ISqlSugarClient Db { get; }

        /// <summary>
        /// 根据Id查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<TEntity> QueryById(object id);

        /// <summary>
        /// 根据Id查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <param name="blnUseCache"></param>
        /// <returns></returns>
        Task<TEntity> QueryById(object id, bool blnUseCache = false);

        /// <summary>
        /// 根据 id数组查询实体list
        /// </summary>
        /// <param name="listIds"></param>
        /// <returns></returns>
        Task<List<TEntity>> QueryByIds(object[] listIds);

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<long> Add(TEntity entity);

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="insertColumns"></param>
        /// <returns></returns>
        Task<long> Add(TEntity entity, Expression<Func<TEntity, object>> insertColumns = null);

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        Task<List<long>> Add(List<TEntity> entities);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> Update(TEntity entity);
        /// <summary>
        /// 根据条件更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        Task<bool> Update(TEntity entity, string where);
        /// <summary>
        /// 更新匿名类型
        /// </summary>
        /// <param name="operateAnonymousObjects"></param>
        /// <returns></returns>
        Task<bool> Update(object operateAnonymousObjects);
        /// <summary>
        /// 更新集合
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        Task<bool> Update(List<TEntity> entities);
        /// <summary>
        /// 更新集合
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        Task<bool> Update(string sql, SugarParameter[] parameters = null);

        /// <summary>
        /// 根据where条件更新指定列
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="listColumns"></param>
        /// <param name="listIgnoreColumns"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        Task<bool> Update(TEntity entity, List<string> listColumns = null, List<string> listIgnoreColumns = null, string where = "");

        /// <summary>
        /// 根据id 删除某一实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> DeleteById(object id);

        /// <summary>
        /// 根据对象删除某一实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> Delete(TEntity entity);

        /// <summary>
        /// 根据id数组删除实体list
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        Task<bool> DeleteByIds(object[] ids);

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        Task<List<TEntity>> Query();

        /// <summary>
        /// 根据表达式条件查询
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 根据表达式查询，返回指定对象模型
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression);

        /// <summary>
        /// 根据表达式和查询条件进行 排序 查询 返回指定类型模型
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="expression"></param>
        /// <param name="whereExpression"></param>
        /// <param name="orderByField"></param>
        /// <returns></returns>
        Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByField);

        /// <summary>
        /// 根据表达式和排序字段查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByField"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string orderByField);

        /// <summary>
        /// 查询 根据条件表达式和排序表达式
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc);

        /// <summary>
        /// 根据字符串条件和排序 查询
        /// </summary>
        /// <param name="where"></param>
        /// <param name="orderByField"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(string where, string orderByField);

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="intTop"></param>
        /// <param name="orderByField"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, string orderByField);
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="where"></param>
        /// <param name="intTop"></param>
        /// <param name="orderByField"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(string where, int intTop, string orderByField);

        Task<List<TEntity>> QuerySQL(string sql, SugarParameter[] parameters = null);

        Task<DataTable> QueryTable(string sql, SugarParameter[] parameters = null);

        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByField);

        Task<List<TEntity>> Query(string where, int pageIndex, int pageSize, string orderByField);

        Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFidels = null);

        /// <summary>
        /// 三连表查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="joinExpression"></param>
        /// <param name="selectExpression"></param>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(
            Expression<Func<T, T2, T3, object[]>> joinExpression,
            Expression<Func<T, T2, T3, TResult>> selectExpression,
            Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new();

        /// <summary>
        /// 两连表查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="joinExpression"></param>
        /// <param name="selectExpression"></param>
        /// <param name="whereExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        Task<PageModel<TResult>> QueryTabsPage<T, T2, TResult>(
            Expression<Func<T, T2, object[]>> joinExpression,
            Expression<Func<T, T2, TResult>> selectExpression,
            Expression<Func<TResult, bool>> whereExpression,
            int pageIndex, int pageSize, string orderByFields = null);

        /// <summary>
        /// 两连表查询 分页 分组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="joinExpression"></param>
        /// <param name="selectExpression"></param>
        /// <param name="whereExpression"></param>
        /// <param name="groupExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        Task<PageModel<TResult>> QueryTabsPage<T, T2, TResult>(
           Expression<Func<T, T2, object[]>> joinExpression,
           Expression<Func<T, T2, TResult>> selectExpression,
           Expression<Func<TResult, bool>> whereExpression,
           Expression<Func<T, object>> groupExpression,
           int pageIndex, int pageSize, string orderByFields = null);
    }
}
